﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Facebook;

namespace WebApp.Models
{
    public class Post : FeedItem
    {
        public class Ref : Post
        {
            public Ref(String id)
            {
                this.id = id;
            }

            public void Populate(String authToken)
            {
                this.authToken = authToken;
                FacebookClient fbc = new FacebookClient(authToken);
                dynamic post = fbc.Get(Id);
                Populate(post);
                
            }

            internal void Populate(dynamic post)
            {
                id = post["id"];

                String ownerId = post["from"]["id"];
                String ownerName = post["from"]["name"];
                from = new Person.Ref(ownerId, ownerName);

                type = post["type"];

                if (post.ContainsKey("message"))
                {
                    message = post["message"];
                }

                if (post.ContainsKey("story"))
                {
                    story = post["story"];
                }
                if (type == "photo")
                {
                    picture = post["picture"];
                    link = post["link"];
                    photo = GetPostPhoto(authToken);
                }
                
                created_time = DateTime.Parse(post["created_time"]);
            }
        }

        #region Fields

        private String id = null;
        private Person.Ref from = null;
        private String message = null;
        private String story = null;
        private String type = null;
        private String picture = null;
        private String link = null;
        private Photo photo = null;
        private DateTime created_time = DateTime.Now;
        private String authToken;

        #endregion

        #region Properties

        public String Id
        {
            get { return id; }
        }

        public Person.Ref From
        {
            get { return from; }
        }

        public String Message
        {
            get { return message; }
        }

        public String Story
        {
            get { return story; }
        }

        public String Type
        {
            get { return type; }
        }

        public String Picture
        {
            get { return picture; }
        }

        public String Link
        {
            get { return link; }
        }

        public Photo Photo
        {
            get { return photo; }
        }
        
        public DateTime CreatedTime
        {
            get { return created_time; }
        }
        
        #endregion

        public Post(String id, Person.Ref from, String message,
            String type, DateTime created_time)
        {
            this.id = id;
            this.from = from;
            this.message = message;
            this.created_time = created_time;
        }

        private Post()
        {
        }

        public static Post Populate(String id, String authToken)
        {
            FacebookClient fbc = new FacebookClient(authToken);
            dynamic post = fbc.Get(id);
            return ParseObject(post);
        }

        public static List<Post> PopulateList(String rootId, String authToken)
        {
            FacebookClient fbc = new FacebookClient(authToken);
            dynamic posts = fbc.Get(rootId + "/home");
//            dynamic next = fbc.Get(posts["paging"]["next"]);
//            dynamic previous = fbc.Get(posts["paging"]["previous"]);
            posts = posts["data"];
            List<Post> result = new List<Post>();


            foreach (dynamic post in posts)
            {
                result.Add(ParseObject(post));
            }

            //posts = posts["
            return result;
        }

        public static Post ParseObject(dynamic post)
        {
            String id = post["id"];
            Post.Ref result = new Post.Ref(id);
//            result.Populate(post);
            return result;
        }

        public Photo GetPostPhoto(String authToken)
        {
            FacebookClient fbc = new FacebookClient(authToken);
            Photo.Ref photoRef;

            dynamic photo =  fbc.Get(Id );
            photoRef = new Photo.Ref(photo["object_id"]);
            photoRef.Populate(authToken);
            return photoRef;
        }
    }
}